package com.lvtulife.common.util;

import java.io.*;

/**
 * 项目部署工具
 * 负责把相关的文件复制到指定的目录
 */
public class DeployUtil {
    public static void main(String[] args) {
        String sourcePath = "H:\\workspace\\lvtulife\\project\\idea\\lvtulife-paas\\";
        String targetPath = "C:\\Users\\Administrator\\Desktop\\VM-update\\update\\common\\";
        String targetPath2 = "C:\\Users\\Administrator\\Desktop\\VM-update\\update\\service\\";
        String targetPath3 = "C:\\Users\\Administrator\\Desktop\\VM-update\\update\\webapp\\";

        String[] commonNames = new String[]{
                "ltu-common|(N)-1.0.jar",
                "ltu-common-config|(N)-1.0.jar",
                "ltu-common-core|(N)-1.0.jar",
                "ltu-common-web|(N)-1.0.jar"
        };

        String[] serviceNames = new String[]{
                "ltu-facade-gtd|(N)-1.0.jar",
                "ltu-facade-mc|(N)-1.0.jar",
                "ltu-service-gtd|(N).jar",
                "ltu-service-mc|(N).jar",
        };

        String[] warNames = new String[]{
                "ltu-web-boss|(N).war"
        };

        try {
            doCopy(sourcePath, targetPath, commonNames);
            doCopy(sourcePath, targetPath2, serviceNames);
            doCopy(sourcePath, targetPath3, warNames);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void doCopy(String sourcePath, String targetPath, String[] commonNames) throws IOException {
        for (int i = 0; i < commonNames.length; i++) {
            String[] names = commonNames[i].split("\\|");
            String name = names[1].replace("(N)", names[0]);
            copyFiles(sourcePath + names[0] + "\\target\\" + name, targetPath + name);
        }
    }

    private static void copyFiles(String sourceFilePath, String targetFilePath) throws IOException {
        if (targetFilePath != null) {
            System.out.println("处理 : " + targetFilePath);

            FileInputStream fileIn = new FileInputStream(sourceFilePath);

            // 删除原有
            File targetFile = new File(targetFilePath);

            if (!targetFile.getParentFile().exists()) {
                targetFile.getParentFile().mkdirs();
            }

            if (targetFile.exists()) {
                targetFile.delete();
            }
            targetFile.createNewFile();

            // 复制新文件
            FileOutputStream fileOut = new FileOutputStream(targetFile);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = fileIn.read(buf)) != -1) {
                fileOut.write(buf, 0, len);
            }
            fileIn.close();
            fileOut.close();
        }
    }
}
